package com.xrz.sxm.aj.activity;

import android.content.Intent;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity.SerializableMap;
import com.xrz.sxm.aj.entity.Test;
import com.xrz.sxm.aj.entity.TestItem;

import java.util.ArrayList;
import java.util.List;

public class Act_chat extends BaseAct {

	@ViewInject(R.id.barchat)
	private BarChart m_BarChart;

	// 柱状图数据集
	private ArrayList<BarEntry> m_Bar_vaules;
	private List<Test> m_Tests;
	private SerializableMap tmpmap;

	@Override
	protected int setView() {
		return R.layout.layout_char;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(tmpmap != null){
			m_Bar_vaules = new ArrayList<BarEntry>();
			showData(tmpmap);
		}
	}

	@Override
	protected void init() {
		m_BarChart.setDrawYValues(true);
		m_BarChart.setDescription("");
		// 最多显示60条数据
		m_BarChart.setMaxVisibleValueCount(60);
		m_BarChart.set3DEnabled(false);
		m_BarChart.setPinchZoom(false);
		m_BarChart.setDrawBarShadow(false);
		m_BarChart.setDrawVerticalGrid(false);
		m_BarChart.setDrawHorizontalGrid(false);
		m_BarChart.setDrawGridBackground(false);
		m_BarChart.setValueTextSize(20f);

		tmpmap = (SerializableMap) getIntent().getSerializableExtra("data");
	}

	private void showData(final SerializableMap tmpmap) {
		ArrayList<String> xVals = new ArrayList<String>();
		m_Tests = tmpmap.getMap();
		BarEntry barEntry;
		int index = 0;
		for (Test test : m_Tests) {
			List<TestItem> list = test.list;
			int count = 0;
			for (TestItem testItem : list) {
				switch (testItem.selectIndex) {
					case 0:
						count += 10;
						break;
					case 1:
						count += 8;
						break;
					case 2:
						count += 5;
						break;
					case 3:
						count += 2;
						break;
					case 4:
						count += 0;
						break;
				}
			}
			xVals.add(list.get(0).parentType);
			barEntry = new BarEntry(count, index);
			m_Bar_vaules.add(barEntry);
			index++;
		}

		BarDataSet set1 = new BarDataSet(m_Bar_vaules, "15以下为正常，15-25为亚健康，25以上为疾病(点击柱状图查看调理方案)");
		set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
		set1.setBarSpacePercent(50);
		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(set1);
		BarData data = new BarData(xVals, dataSets);
		m_BarChart.setData(data);
		m_BarChart.invalidate();

		Legend mLegend = m_BarChart.getLegend();
		mLegend.setTextSize(12);

		mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);//位置 RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER,
		// RIGHT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER or PIECHART_CENTER (PieChart only), ... and more
		mLegend.setForm(Legend.LegendForm.SQUARE);// 样式SQUARE, CIRCLE or LINE.
		mLegend.setFormSize(10f);// 大小 单位dp
		mLegend.setYEntrySpace(20f);// 条目的垂直间距
		XLabels xLabels = m_BarChart.getXLabels();
		xLabels.setPosition(XLabelPosition.BOTTOM);
		xLabels.setCenterXLabelText(true);
		xLabels.setTextSize(20);
		xLabels.setSpaceBetweenLabels(0);
		m_BarChart.animateY(2500);
		m_BarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
			@Override
			public void onValueSelected(Entry entry, int i) {
				int index=entry.getXIndex();
				if(index==8){
					return ;
				}else{
					Intent intent = new Intent(Act_chat.this, Act_ChartDetail.class);
					intent.putExtra("index", entry.getXIndex());
					intent.putExtra("data", tmpmap);
					startActivity(intent);
				}
			}

			@Override
			public void onNothingSelected() {

			}
		});
	}

	@Override
	protected String setTopTitle() {
		return "测试结果";
	}

}
